package com.arpgalaxy.spring.boot.autoconfigure.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author arpgalaxy
 * @date 2020/9/27
 * @email 13605560342@163.com
 * @description
 */
public class ArpgalaxyShiroRealm extends AuthorizingRealm {

    @Autowired
    ArpgalaxyShiroRealmService arpgalaxyShiroRealmService;
    public String getName(){
        return "arpgalaxyShiroRealm";
    }
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof ShiroAuthenticatingToken;
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        AuthenticationEntity entity=(AuthenticationEntity)principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.setStringPermissions(arpgalaxyShiroRealmService.doGetPermissions(entity));
        simpleAuthorizationInfo.setRoles(arpgalaxyShiroRealmService.doGetRoles(entity));
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        return new SimpleAuthenticationInfo(arpgalaxyShiroRealmService.doGetAuthenticationInfo(token),token.getPrincipal(),getName()) ;
    }

}
